package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.entity.Employee;
import com.itheima.serivce.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R log(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(wrapper);

        if (one == null) {
            return R.error("账号错误");
        }

        if (!one.getPassword().equals(s)) {
            return R.error("密码错误");
        }

        if (one.getStatus() != 1) {
            return R.error("账号被禁用");
        }

        //设置session
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);

    }

    //退出功能
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增功能
    @PostMapping
    public R save1(HttpServletRequest request, @RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //设置修改人id
//        Long id = (Long) request.getSession().getAttribute("employee");
//        //设置修改人信息
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);

        employeeService.save(employee);
        return R.success("保存成功");
    }

    //添加分页查询
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        Page page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        /*wrapper.like(name != null, Employee::getName, name);*/

         wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
         //根据时间排序
        wrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page1, wrapper);

        return R.success(page1);
    }


    // 查詢 修改二合一
    //修改状态
    @PutMapping
    public R update(HttpServletRequest request, @RequestBody Employee employee) {

//        Long employee1 = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(employee1);

        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    //根據id查詢  数据
    @GetMapping("{id}")
    public R getBy(@PathVariable("id") Long id) {
        Employee byId = employeeService.getById(id);
        if (byId != null) {
            return R.success(byId);
        }
        return R.error("用户不存在");
    }

}




